<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>localStorage</title>
</head>

<body>

    <div id="dome">

    </div>
</body>
<script>

    //  容错 ---》 

    // 封装 localStorage
    try {
        localStorage.setItem('username', "xiao123")
    } catch (error) {

    }
    //  js 特性

    // localStorage
    // 存  key  -- value 
    // localStorage.setItem('username', "xiao123")
    // localStorage.setItem("username1", "xiao123456999999")
    // localStorage.setItem("username3","xiao3")
    // 取 key
    // localStorage.getItem("username") 
    // console.log(localStorage.username)
    // document.getElementById("dome").innerHTML = localStorage.getItem("username")
    // 改
    // localStorage.setItem("username","xiao123456999999")
    // 删
    // 删除全部
    // localStorage.clear()
    // 指定某个key删除
    // localStorage.removeItem('username3')
    // console.log(localStorage.username)
    // console.log(localStorage)

    // key() 得到某个索引的key
    // console.log(localStorage.key(0)) //


    // 注意
    // localstorage存进去的值会变成字符串
    // Number Boolean Object等 统统转为字符串
    // localStorage.setItem('age', 18) // 
    // console.log(Number(localStorage.getItem('age'))  + 10)


    // let obj = {a:1,b:2}
    // localStorage.setItem("myobj",obj)
    // let getobj = localStorage.getItem("myobj")
    // console.log(getobj) // [object Object] 

    // localStorage 怎么存对象
    /*
    1、先用JSON.stringify()把对象转为字符串
    2、取出数据时要用JSON.parse() 把字符串转为对象
    */
    // let obj = { a: 1, b: 2 }
    // localStorage.setItem('s', JSON.stringify(obj))// 转为字符串
    // let gS = localStorage.getItem('s')
    // console.log(JSON.parse(gS).a)

    // let arrx= [{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 }]
</script>

</html>